import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:cached_network_image/cached_network_image.dart';
import './size.dart';
import 'package:get/get.dart';

class SImage extends StatelessWidget {
  final String? url;
  final double? width;
  final double? height;
  final BoxFit fit;
  final Alignment alignment;
  final Color color;
  final Color bgColor;
  final Color errorColor;
  final BlendMode colorBlendMode;
  final ColorFilter? colorFilter;
  final bool loading;
  final bool compress;
  final BorderRadius? borderRadius;
  final int? maxWidthDiskCache;
  final int? maxHeightDiskCache;

  const SImage(this.url, {
    Key? key,
    this.width,
    this.height,
    this.fit = BoxFit.cover,
    this.compress = true,
    this.alignment = Alignment.center,
    this.color = const Color(0xff2f2e3c),
    this.bgColor = const Color(0xffe6e7e8),
    this.errorColor = const Color(0xffb3b3b3),
    this.colorBlendMode = BlendMode.dst,
    this.colorFilter,
    this.loading = true,
    this.borderRadius,
    this.maxWidthDiskCache,
    this.maxHeightDiskCache,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Widget _err = Container(
      width: width,
      height: height,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        borderRadius: borderRadius ?? BorderRadius.circular(0),
        color: bgColor,
      ),
      child: Text("img_hint".tr,
        style: TextStyle(fontSize: width == null ? 36.r : width! / 8, color: errorColor),
      ),
    );

    if (url == null || !Uri
        .parse(url!)
        .isAbsolute) {
      return _err;
    }

    Widget placeholder = Container(
      width: width,
      height: height,
      alignment: Alignment.center,
      decoration: BoxDecoration(
        borderRadius: borderRadius ?? BorderRadius.circular(0),
        color: bgColor,
      ),
      // child: CupertinoActivityIndicator(),
    );

    late Widget img;

    if (kIsWeb) {
      return ClipRRect(
        borderRadius: borderRadius ?? BorderRadius.circular(0),
        child: Image.network(
          url!,
          width: width,
          height: height,
          fit: fit,
          alignment: alignment,
          colorBlendMode: BlendMode.dst,
        ),
      );
    }

    img = CachedNetworkImage(
      imageUrl: url!,
      width: width,
      height: height,
      fadeOutDuration: Duration(milliseconds: 50),
      fadeInDuration: Duration(milliseconds: 50),
      imageBuilder: (context, imageProvider) =>
          Container(
            decoration: BoxDecoration(
              borderRadius: borderRadius ?? BorderRadius.circular(0),
              image: DecorationImage(
                image: imageProvider,
                fit: fit,
                alignment: alignment,
                colorFilter: colorFilter,
              ),
            ),
          ),
      placeholder: (context, url) => placeholder,
      errorWidget: (context, url, error) => _err,
      maxWidthDiskCache: compress ? (maxWidthDiskCache ?? 1200.r.toInt()) : null,
      maxHeightDiskCache: compress ? (maxHeightDiskCache ?? 1200.r.toInt()) : null,
    );

    return img;
  }
}
